define(["require", "exports", "./virtualTreeChildren", "./util"], function (require, exports, virtualTreeChildren_1, util_1) {
    "use strict";
    exports.__esModule = true;
    exports.childrenBuilder = exports.baseChildrenBuilder = exports.original = exports.fragment = exports.refreshInit = void 0;
    function refreshInit(result) {
        if (result) {
            return function () {
                var destroy = result();
                if (destroy) {
                    if (typeof (destroy) == 'function') {
                        return destroy;
                    }
                    else {
                        console.warn("ChildLife\u8FD4\u56DE\u4E0D\u662F\u51FD\u6570\uFF0C\u4E0D\u89C6\u4E3A\u751F\u547D\u5468\u671F\u51FD\u6570", destroy, this);
                    }
                }
            };
        }
        return result;
    }
    exports.refreshInit = refreshInit;
    /**
     * 片段自定义组件
     * @param config
     * @param children
     * @returns
     */
    function fragment(p) {
        var init = refreshInit(p.init);
        var children = p.children;
        return function (parent, me) {
            var outs = [];
            if (init) {
                outs.push(init);
            }
            for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
                var child = children_1[_i];
                var out = baseChildrenBuilder(me, child, parent);
                if (out) {
                    outs.push(out);
                }
            }
            return (0, util_1.getAsOne)(outs);
        };
    }
    exports.fragment = fragment;
    /**
     * 原生组件
     * @param config
     * @param children
     */
    function original(p) {
        var init = refreshInit(p.init);
        var children = p.children;
        return function (parent) {
            for (var _i = 0, children_2 = children; _i < children_2.length; _i++) {
                var child = children_2[_i];
                parent.push(child);
            }
            return init;
        };
    }
    exports.original = original;
    function baseChildrenBuilder(me, children, parent) {
        return children(parent.newChildAtLast(), me);
    }
    exports.baseChildrenBuilder = baseChildrenBuilder;
    function childrenBuilder(me, x, children) {
        var outs = [];
        var root = virtualTreeChildren_1.VirtualChild.newRootChild(x);
        for (var _i = 0, children_3 = children; _i < children_3.length; _i++) {
            var child = children_3[_i];
            var out = baseChildrenBuilder(me, child, root);
            if (out) {
                outs.push(out);
            }
        }
        return (0, util_1.getAsOne)(outs);
    }
    exports.childrenBuilder = childrenBuilder;
});
